package com.tjetc.controller;

import com.tjetc.domain.Classify;
import com.tjetc.service.ClassifyService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    @RequestMapping("/all")
    public List<Classify> findAllClassify() {
        return classifyService.classifyAll();
    }

    @PostMapping("/find")
    public List<Classify> findClassify(@RequestBody Find find) {
        Map<String, Object> params = new HashMap<>();
        String code = find.getCode();
        String fourth = find.getFourth();
        String city = null;
        String classify = null;
        String time = null;
        if (!Objects.equals(find.getCity(), "全国")) {
            city = find.getCity();
        }
        if (!Objects.equals(find.getClassify(), "全部")) {
            classify = find.getClassify();
        }
        if (!Objects.equals(find.getTime(), "全部")) {
            time = find.getTime();
        }
        params.put("city", city);
        params.put("classify", classify);
        params.put("time", time);
        params.put("start", 0); // 从第0条记录开始
        params.put("length", 10); // 获取10条记录
        return classifyService.classifyFind(params);
    }

    @PostMapping("/findCount")
    public int countClassifyFind(@RequestBody countFind find) {
        Map<String, Object> params = new HashMap<>();
        String city = null;
        String classify = null;
        String time = null;
        if (!Objects.equals(find.getCity(), "全国")) {
            city = find.getCity();
        }
        if (!Objects.equals(find.getClassify(), "全部")) {
            classify = find.getClassify();
        }
        if (!Objects.equals(find.getTime(), "全部")) {
            time = find.getTime();
        }
        params.put("city", city);
        params.put("classify", classify);
        params.put("time", time);
        return classifyService.countClassifyFind(params);
    }

    @RequestMapping("/count")
    public int countRowsInTable() {
        return classifyService.countRowsInTable();
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class Find {
        private String code;
        private String fourth;
        private String city;
        private String classify;
        private String time;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class countFind {
        private String city;
        private String classify;
        private String time;
    }
}

